import axios from "axios";
import router from "../router";
//基础路径 
const newAxios= axios.create({
    baseURL: 'http://47.98.128.191:3000',
    timeout:5000
})
//设置axios请求拦截器
newAxios.interceptors.request.use((config) => {
        //拦截成功的响应
    const token = localStorage.token;
    config.headers.Authorization = token;
    return config;

    }, (err) => {
        //请求失败的响应
    return err
    }

)

//设置axios响应拦截器
newAxios.interceptors.response.use(res => {
    //请求成功的响应
    return res.data

}, (err) => {
    //请求失败的响应
    if (err && err.response && err.response.status) {
    switch (err.response.status) {
        case 401:
            alert('你还没登陆呢');
            router.push('/login');
            break;
        case 404:
            alert('资源找不到');
           err.message = '资源找不到'
            break;
        case 500:
            alert('服务器出错');
           err.message = '服务器出错'
            break;
    
        default:
            break;
    }


    } else {
        
return Promise.reject(err)

}


}

)

 export default newAxios